package idiet.view.cell;

import idiet.bean.MealRecipe;
import idiet.bean.Unit;

import java.math.BigDecimal;

import org.eclipse.jface.viewers.ICellModifier;
import org.eclipse.jface.viewers.TableViewer;
import org.eclipse.swt.widgets.TableItem;

public class MealAddMealRecipe implements ICellModifier {
	private TableViewer tableViewer;
	
	public MealAddMealRecipe(TableViewer tableViewer){
		this.tableViewer=tableViewer;
	}
	@Override
	public boolean canModify(Object element, String property) {
		// TODO Auto-generated method stub
		if(property.equals("name")){
			return false;
		}else{
			return true;
		}
	}

	@Override
	public Object getValue(Object element, String property) {
		// TODO Auto-generated method stub
		MealRecipe item=(MealRecipe)element;
		if(property.equals("qty")){
			BigDecimal qty=item.getQty();
			return qty==null?"0":qty.toString();
		}else if(property.equals("unit")){
			Unit unit=item.getUnit();
			return unit==null?"":unit.getName();
		}
		return null;
	}

	@Override
	public void modify(Object element, String property, Object value) {
		// TODO Auto-generated method stub
		TableItem item=(TableItem)element;
		MealRecipe recipe=(MealRecipe)item.getData();
		if(property.equals("qty")){
			String newValue=value.toString();
			if(newValue.trim().equals("")){
				return;
			}else{
				recipe.setQty(new BigDecimal(newValue));
			}
		}else if(property.equals("unit")){
			System.out.println(value);
		}
		tableViewer.update(recipe, null);
	}

}
